package cn.rkylin.oms.refund.adapter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import cn.rkylin.oms.refund.domain.EcRefundItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import cn.rkylin.core.IDataBaseFactory;
import cn.rkylin.oms.refund.dao.IEcRefundDAO;
import cn.rkylin.oms.refund.domain.EcRefund;
import cn.rkylin.oms.refund.vo.EcRefundVO;

@Service("taobaoRefundService")
public class TaobaoRefundService {

    @Resource(name = "redisTemplate")
    private RedisTemplate<Serializable, Serializable> redisTemplate;
    
    @Autowired
    private IDataBaseFactory dao;
    @Autowired
    private IEcRefundDAO ecRefundDAO;
    
    public int update(String updateStatement, EcRefund ecRefund) throws Exception {
        int result = dao.update(updateStatement, ecRefund);
        //redis 更新 按照交易号放置
       /* if (result!=0){
            updateRedis(ecRefund);
        }*/
        return result;
    }
    
    public int insert(String pageStatement, EcRefund ecRefund) throws Exception {
        int result = dao.insert(pageStatement, ecRefund);
      /*  if (result!=0){
            updateRedis(ecRefund);
        }*/
        
        return result;
    }
    
    private void updateRedis(EcRefund ecRefund) throws Exception{
        
        List<String> list = null;
        //redis 更新
        ValueOperations<Serializable, Serializable> valueops = redisTemplate.opsForValue();
        list = (ArrayList<String>)valueops.get("淘宝-Refund-EcRefundCode:"+ecRefund.getEcRefundCode());
        if (list!=null && list.size()>0){
            boolean flg = false; 
            for (int i = 0 ; i < list.size() ; i++){
                if (list.get(i).equals(ecRefund.getEcRefundCode())){
                    flg = true;
                    break;
                }
            }
            if (!flg){
                list.add(ecRefund.getEcRefundCode());
            }
        } else {
            list = new ArrayList<String>();
            list.add(ecRefund.getEcRefundCode());
        }
        valueops.set("淘宝-Refund-EcRefundCode:"+ecRefund.getEcRefundCode(), (Serializable)list); 
    }
    
    
    public List<EcRefund> getEcRefundByCode(String ecRefundCode) throws Exception{
        boolean flg = false; 
        EcRefund ecRefund = null;
        List<String> list = null;
        List<EcRefund> listRefund = new ArrayList<EcRefund>();
        ValueOperations<Serializable, Serializable> valueops = redisTemplate.opsForValue();
        
        list = (ArrayList<String>)valueops.get("淘宝-Refund-EcRefundCode:"+ecRefundCode);
        if (list==null){
            list = new ArrayList<String>();
        }
        for (int i = 0 ; i < list.size() ; i++){
            ecRefund = (EcRefund)dao.find("selectByPrimaryKeyEcRefund", list.get(i));
            if (ecRefund!=null){
                listRefund.add(ecRefund);    
            }
        }
        
        if (listRefund==null || listRefund.size()<=0){
            Map<String,String> reqMap = new HashMap<String,String>();
            reqMap.put("ecRefundCode", ecRefundCode);
            listRefund = dao.findForList("selectByRefundCode", reqMap);
            if (listRefund!=null && listRefund.size()>0){
                for (int i = 0 ; i < listRefund.size() ; i++){
                    list.add(listRefund.get(i).getEcRefundId());
                }
                valueops.set("淘宝-Refund-EcRefundCode:"+ecRefundCode, (Serializable)list); 
            }
        }
        
        if (listRefund!=null && listRefund.size()>0){
            //redis shopid是key，内容是交易号
            List<String> listShopKye = null;
            EcRefund parentEcRefund = (EcRefund)dao.find("selectByPrimaryKeyEcRefund", listRefund.get(0).getParentRefundId());
            listShopKye = (ArrayList<String>)valueops.get("淘宝-Refund-shopId:"+parentEcRefund.getShopId());
            if (listShopKye==null){
                listShopKye = new ArrayList<String>();
            }
            flg = false;
            for (int i = 0 ; i < listShopKye.size() ; i++){
                if (listShopKye.get(i).equals(ecRefundCode)){
                    flg = true;
                    break;
                }
            }
            if (!flg){
                listShopKye.add(ecRefundCode);    
            }
            valueops.set("淘宝-Refund-shopId:"+parentEcRefund.getShopId(), (Serializable)listShopKye);
        }
        
        
        return listRefund;
    }
    
    /**
     * getEcRefund4AutoDownload:根据查询条件获取即将执行自动下载的平台退款单列表. <br/>
     *
     * @author zhanghao
     * @param ecRefundVO
     * @return
     */
    public List<EcRefundVO> getEcRefund4AutoDownload(EcRefundVO ecRefundVO) throws Exception{
        return ecRefundDAO.selectEcRefund4AutoDownload(ecRefundVO);
    }
    /**
     * getEcRefund4AutoUpdate:根据查询条件获取即将执行自动更新的平台退款单列表. <br/>
     *
     * @author zhanghao
     * @param ecRefundVO
     * @return
     */
    public List<EcRefundVO> getEcRefund4AutoUpdate(EcRefundVO ecRefundVO) throws Exception{
        return ecRefundDAO.selectEcRefund4AutoUpdate(ecRefundVO);
    }
    
    /**
     * updateBatchEcRefundInsertStatus:批量更新平台退款单的进系统状态. <br/>
     *
     * @author zhanghao
     * @param ecRefundList
     * @return
     * @throws Exception
     */
    public int updateBatchEcRefundInsertStatus(List<EcRefundVO> ecRefundList) throws Exception {
        return ecRefundDAO.updateBatchInsertStatus(ecRefundList);
    }

    /**
     * updateEcRefundInsertStatus:批量更新平台退款单的进系统状态. <br/>
     *
     * @author zhanghao
     * @param ecRefundList
     * @return
     * @throws Exception
     */
    public int updateEcRefundInsertStatus(EcRefundVO ecRefundVO) throws Exception {
        List<EcRefundVO> ecRefundList = new ArrayList<EcRefundVO>();
        ecRefundList.add(ecRefundVO);
        return ecRefundDAO.updateBatchInsertStatus(ecRefundList);
    }
    
    /**
     * updateReturnInsertStatus:批量更新平台退款单的退货单插入状态. <br/>
     *
     * @author zhanghao
     * @param ecRefundList
     * @return
     * @throws Exception
     */
    public int updateReturnInsertStatus(EcRefundVO ecRefundVO) throws Exception {
        List<EcRefundVO> ecRefundList = new ArrayList<EcRefundVO>();
        ecRefundList.add(ecRefundVO);
        return ecRefundDAO.updateBatchReturnInsertStatus(ecRefundList);
    }

    /**
     * 根据refundId获取EcRefund
     * @param refundId
     * @return
     * @throws Exception
     */
    public EcRefund selectEcRefundByRefundId(String refundId) throws Exception{
        EcRefund ecRefund = (EcRefund)dao.find("selectEcRefundByRefundId",refundId);
        return ecRefund;
    }
    
}
